<%--
  ** Copyright(c) 2005 BroadVision, Inc. All rights reserved.
  **
  ** This software is copyrighted.  Under the copyright laws, this software
  ** may not be copied, in whole or in part, without prior written consent
  ** of BroadVision, Inc. or its assignees. This software is provided under
  ** the terms of a license between BroadVision and the recipient, and its
  ** use is subject to the terms of that license.
  **
  ** This software may be protected by one or more U.S. and International
  ** patents. Certain applications of BroadVision One-To-One software are
  ** covered by U.S. patent 5,710,887.
  **
  ** TRADEMARKS: BroadVision and the BroadVision logo are registered
  ** trademarks, and BroadVision One-To-One is a trademark of BroadVision,
  ** Inc. IONA and Orbix are trademarks of IONA Technologies, Ltd. RSA,
  ** MD5, and RC2 are trademarks of RSA Data Security, Inc. All other
  ** trademarks, service marks, and trade names belong to their respective
  ** owners. BroadVision, Inc. disclaims any proprietary interest in the
  ** marks and names of others.
  --%>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>

<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.TreeMap" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="org.apache.struts.Globals" %>
<%@ page import="org.apache.struts.action.ActionMapping" %>
<%@ page import="com.broadvision.report.admin.ReportConfig" %>
<%@ page import="com.broadvision.report.admin.ProcessConfig" %>
<%@ page import="com.broadvision.report.admin.CaptureConfig" %>
<%@ page import="com.broadvision.report.admin.CheckpointConfig" %>
<%@ page import="com.broadvision.report.admin.MetaDataConfig" %>
<%@ page import="com.broadvision.ssp.client.SelfServiceProcess" %>

<c:set var="view" value="${reportTaskEditView}"/>
<c:set var="reportConfig" value="${runtimeReportConfigModel}"/>
<%
  final String ctxPath = request.getContextPath();
  final ReportConfig reportModel =
          (ReportConfig)request.getAttribute("reportConfig");

  // get process name: work around issue with non-public base class
  //
  final SelfServiceProcess procSspModel =
          (SelfServiceProcess)request.getAttribute("procSspModel");
  if (procSspModel != null)
    pageContext.setAttribute("procName", procSspModel.getName());

  // get checkpoint/capture config, etc.
  //
  final Map view = (Map)pageContext.getAttribute("view");
  final CheckpointConfig chkptInfo = (CheckpointConfig)view.get("chkptInfo");
  final Map fixedData = (chkptInfo == null) ? null : chkptInfo.getFixedData();
  final CaptureConfig captureInfo = (CaptureConfig)view.get("captureInfo");
  final Map varData = (captureInfo == null) ? null : captureInfo.getVariables();

  // set up pararmeters for pagetab URLs based on request attributes
  //
  final HashMap map = new HashMap();
  final String attrs[] = {
                "taskType",
                "taskIndex",
                "taskAlias",
                "procIndex",
                "flowIndex"
            };
  int i;
  for (i = 0; i < attrs.length; ++i)
    map.put(attrs[i], request.getAttribute(attrs[i]));
  pageContext.setAttribute("pagetabParam", map, PageContext.PAGE_SCOPE);

  final String trClassList[] = { "listEven", "listOdd" };
  pageContext.setAttribute("listSwitch", trClassList);
%>

<c:set var="deleteImg">
  <img src="<%=ctxPath%>/bp/images/action/icon_delete.gif"
       width="15" height="15" border="0" class="actionIcon"
       alt='<bean-el:message bundle="cm.messages" key="image.alt.delete"/>'
       title='<bean-el:message bundle="cm.messages" key="image.alt.delete"/>'
  >
</c:set>

<script language="Javascript">
  function setSelection(field, value)
  {
    var choices = field.options;
    var count = choices.length;
    var i;
    for (i = 0; i < count; ++i) {   // >
      if (choices[i].value == value) {
        choices[i].selected = true;
        break;
      }
    }
  }

  function clearSelection(field)
  {
    var choices = field.options;
    choices.selectedIndex = -1;
  }

  function doSetLogger(form)
  {
    var loggerField = form.logger;
    var oldLogger = '<c:out value="${view.chkptInfo.logger}"/>';
    var msg;

    if (oldLogger == "") {
      if (loggerField.options.selectedIndex <= 0)   // >
        return false;
      msg = '<bean-el:message key="bp.report.taskEdit.${view.chkptType}.confirm.enable" bundle="bp.messages"/>';
      if (!confirm(msg)) {
        clearSelection(loggerField);
        return false;
      }
    }
    else {
      if (loggerField.options.selectedIndex <= 0) { // >
        msg = '<bean-el:message key="bp.report.taskEdit.${view.chkptType}.loggerRequired" bundle="bp.messages"/>';
        alert(msg);
        setSelection(loggerField, oldLogger);
        return false;
      }
      msg = '<bean-el:message key="bp.report.taskEdit.logger.confirm.change" bundle="bp.messages"/>';
      if (!confirm(msg)) {
        setSelection(loggerField, oldLogger);
        return false;
      }
    }

    form.submit();
    return true;
  }

  function doSetMetaData(form)
  {
    var metaField = form.metadata;
    var oldMeta = '<c:out value="${view.chkptInfo.metaData}"/>';

    if (oldMeta == "") {
      if (metaField.options.selectedIndex <= 0) // >
        return false;
      msg = '<bean-el:message key="bp.report.taskEdit.metadata.confirm.change" bundle="bp.messages"/>';
      if (!confirm(msg)) {
        clearSelection(metaField);
        return false;
      }
    }
    else {
      var metaName = '<c:out value="${view.chkptInfo.metaData}"/>';
      if (metaField.options.selectedIndex <= 0) // >
        msg = '<bean-el:message key="bp.report.taskEdit.metadata.confirm.remove" bundle="bp.messages"/>';
      else
        msg = '<bean-el:message key="bp.report.taskEdit.metadata.confirm.change" bundle="bp.messages"/>';
      if (!confirm(msg)) {
        setSelection(metaField, metaName);
        return false;
      }
    }
    form.submit();
    return true;
  }

  function doDelete(form)
  {
    var msg =
      '<bean-el:message key="bp.report.taskEdit.${view.chkptType}.confirm.delete" bundle="bp.messages"/>';

    if (!confirm(msg))
      return false;

    form.submit();
    return true;
  }

  function ensureLabelNotInUse(labelField)
  {
    // TODO: check if label is already used in fixed data, variable, or
    // meta data
    //
    return true;
  }

  function doAddFixedData(form)
  {
    var labelField = form.dataLabel;
    var valueField = form.dataValue;

    // check if label is empty
    //
    if (labelField.value == "") {
      labelField.focus();
      var msg =
        '<bean-el:message key="bp.report.taskEdit.fixedData.missingLabel" bundle="bp.messages"/>';
      alert(msg);
      return false;
    }

    if (!ensureLabelNotInUse(labelField))
      return false;

    // check if value is empty
    //
    if (valueField.value == "") {
      var msg =
        '<bean-el:message key="bp.report.taskEdit.fixedData.confirm.emptyValue" bundle="bp.messages"/>';
      if (!confirm(msg)) {
        valueField.focus();
        return false;
      }
    }

    return true;
  }

  function doDelFixedData(form)
  {
    var msg =
      '<bean-el:message key="bp.report.taskEdit.fixedData.confirm.delete" bundle="bp.messages"/>';

    if (!confirm(msg))
      return false;

    form.action = '<c:url value="/bp/reportFixedDataDelete.do"/>';
    form.submit();
    return true;
  }

  function doAddVariable(form)
  {
    var labelField = form.varLabel;

    // check if label is empty
    //
    if (labelField.value == "") {
      labelField.focus();
      var msg =
        '<bean-el:message key="bp.report.taskEdit.varData.missingLabel" bundle="bp.messages"/>';

      alert(msg);
      return false;
    }

    if (!ensureLabelNotInUse(labelField))
      return false;

    return true;
  }

  function doDelVariable(form)
  {
    var msg =
      '<bean-el:message key="bp.report.taskEdit.varData.confirm.delete" bundle="bp.messages"/>';

    if (!confirm(msg))
      return false;

    form.action = '<c:url value="/bp/reportVariableDelete.do"/>';
    form.submit();
    return true;
  }
</script>

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <bean-el:message
          key="bp.report.taskEdit.${taskType}.title"
          bundle="bp.messages"
          arg0="${view.taskName}"
        />
      </div>
      <div class="errorMessage"><html-el:errors bundle="bp.messages"/></div>
      <c:if test="${!empty reportConfigStatusKey}">
        <div>
          <bean-el:message key="${reportConfigStatusKey}" bundle="bp.messages"/>
        </div>
      </c:if>
      <div><%@include file="/mc/common/pagetab.jsp"%></div>
      <div class="breadcrumb">
        <c:set var="pathSeparator">
          <bean-el:message
            key="bp.report.taskEdit.pathSeparator" bundle="bp.messages"
          />
        </c:set>
        <c:url var="procUrl" value="/bp/reportEditProcess/${taskType}.do">
          <c:param name="procIndex" value="${procIndex}"/>
        </c:url>
        <a href="<c:out value="${procUrl}"/>">
          <c:out value="${procName}"/>
        </a>
        <c:if test="${flowIndex >= 0}">
          &nbsp;<c:out value="${pathSeparator}" escapeXml="false"/>&nbsp;
          <c:url var="flowUrl" value="/bp/reportEditPageflow/${taskType}.do">
            <c:param name="procIndex" value="${procIndex}"/>
            <c:param name="flowIndex" value="${flowIndex}"/>
          </c:url>
          <a href="<c:out value="${flowUrl}"/>">
            <c:out value="${view.flowDef.friendlyName}"/>
          </a>
        </c:if>
        &nbsp;<c:out value="${pathSeparator}" escapeXml="false"/>&nbsp;
        <c:out value="${view.taskName}"/>
      </div>
      <div class="dataTable">
        <table width="100%">
          <tr width="100%" valign="top">
            <td align="left">
              <c:set var="loggerList" value="${reportConfig.loggerList}"/>
              <c:set var="currentLogger" value=""/>
              <c:if test="${!empty view.chkptInfo}">
                <c:set var="currentLogger" value="${view.chkptInfo.logger}"/>
              </c:if>
              <html-el:form action="/bp/reportSetLogger.do">
                <html-el:hidden property="chkptType" value="${view.chkptType}"/>
                <html-el:hidden property="taskType" value="${taskType}"/>
                <html-el:hidden property="taskIndex" value="${taskIndex}"/>
                <html-el:hidden property="procIndex" value="${procIndex}"/>
                <html-el:hidden property="flowIndex" value="${flowIndex}"/>
                <bean-el:message
                  key="bp.report.taskEdit.logger.label"
                  bundle="bp.messages"
                />
                <html-el:select onchange="return doSetLogger(this.form);"
                                property="logger"
                                value="${currentLogger}">
                  <html-el:option value=""/>
                  <html-el:options collection="loggerList" property="name"/>
                </html-el:select>
              </html-el:form>
            </td>
            <td align="left">
              <c:set var="metaDataList" value="${reportConfig.metaDataList}"/>
              <c:set var="currentMetaData" value=""/>
              <c:if test="${!empty view.chkptInfo}">
                <c:set
                  var="currentMetaData" value="${view.chkptInfo.metaData}"
                />
              </c:if>

              <html-el:form action="/bp/reportSetMetaData.do">
                <html-el:hidden property="chkptType" value="${view.chkptType}"/>
                <html-el:hidden property="taskType" value="${taskType}"/>
                <html-el:hidden property="taskIndex" value="${taskIndex}"/>
                <html-el:hidden property="procIndex" value="${procIndex}"/>
                <html-el:hidden property="flowIndex" value="${flowIndex}"/>
                <bean-el:message
                  key="bp.report.taskEdit.metadata.label"
                  bundle="bp.messages"
                />
                <html-el:select onchange="return doSetMetaData(this.form);"
                                property="metadata"
                                disabled="${empty view.chkptInfo}"
                                value="${currentMetaData}">
                  <html-el:option value=""/>
                  <html-el:options collection="metaDataList" property="name"/>
                </html-el:select>
              </html-el:form>
            </td>
            <c:choose>
              <c:when test="${!empty view.chkptInfo}">
                <td align="right">
                  <html-el:form action="/bp/reportDeleteCheckpoint.do">
                    <html-el:hidden
                      property="chkptType" value="${view.chkptType}"
                    />
                    <html-el:hidden property="taskType" value="${taskType}"/>
                    <html-el:hidden property="taskIndex" value="${taskIndex}"/>
                    <html-el:hidden property="procIndex" value="${procIndex}"/>
                    <html-el:hidden property="flowIndex" value="${flowIndex}"/>
                    <html-el:submit styleClass="inputButton"
                                    property="deleteChkpt"
                                    disabled="${empty view.chkptInfo}"
                                    onclick="return doDelete(this.form);">
                      <bean-el:message
                        key="button.delete" bundle="bp.messages"
                      />
                    </html-el:submit>
                  </html-el:form>
                </td>
              </c:when>
              <c:otherwise>
                <td align="right">
                  <bean-el:message
                    key="bp.report.taskEdit.enable.instructions"
                    bundle="bp.messages"
                  />
                </td>
              </c:otherwise>
            </c:choose>
          </tr>
        </table>
      </div>
      <div>&nbsp;</div>

      <!--
        ************************************************************
        ** Fixed data table.
        -->
      <c:if test="${!empty view.chkptInfo}">
        <div class="pageSubtitle">
          <bean-el:message
            key="bp.report.taskEdit.fixedData.title" bundle="bp.messages"
          />
        </div>
        <table class="dataTable" border="0" cellspacing="1" cellpadding="2"
              summary="fixed data" width="100%">
          <tr class="listHeader">
            <th id="Label:Fixed" align="left">
              <bean-el:message
                key="bp.report.taskEdit.fixedData.header.label"
                bundle="bp.messages"
              />
            </th>
            <th id="Value:Fixed" align="left">
              <bean-el:message
                key="bp.report.taskEdit.fixedData.header.value"
                bundle="bp.messages"
              />
            </th>
            <th id="Actions:Fixed" align="left" nowrap>
              <bean-el:message key="header.actions" bundle="bp.messages"/>
            </th>
          </tr>

          <%
          if (fixedData != null && !fixedData.isEmpty())
            pageContext.setAttribute("fixedData", new TreeMap(fixedData));
          else
            pageContext.removeAttribute("fixedData");
          %>
          <c:choose>
            <c:when test="${empty fixedData}">
              <tr class="listEven">
                <td colspan="3" align="center">
                  <bean-el:message
                    key="bp.report.taskEdit.fixedData.none" bundle="bp.messages"
                  />
                </td>
              </tr>
            </c:when>
            <c:otherwise>
              <c:forEach var="entry" items="${fixedData}" varStatus="index">
                <html-el:form action="/bp/reportFixedDataUpdate.do">
                  <html-el:hidden
                    property="chkptType" value="${view.chkptType}"
                  />
                  <html-el:hidden property="taskType" value="${taskType}"/>
                  <html-el:hidden property="taskIndex" value="${taskIndex}"/>
                  <html-el:hidden property="procIndex" value="${procIndex}"/>
                  <html-el:hidden property="flowIndex" value="${flowIndex}"/>
                  <html-el:hidden property="dataLabel" value="${entry.key}"/>
                  <tr class="<c:out value="${listSwitch[(index.count-1)%2]}"/>">
                    <td headers="Label:Fixed"><c:out value="${entry.key}"/></td>
                    <td headers="Value:Fixed">
                      <html-el:text
                        property="dataValue" value="${entry.value}"
                      />
                    </td>
                    <td headers="Actions:Fixed">
                      <html-el:submit styleClass="inputButton" property="upd">
                        <bean-el:message
                          key="button.update" bundle="bp.messages"
                        />
                      </html-el:submit>
                      <html-el:submit styleClass="inputButton" property="del"
                                      onclick="doDelFixedData(this.form);">
                        <bean-el:message
                          key="button.delete" bundle="bp.messages"
                        />
                      </html-el:submit>
                    </td>
                  </tr>
                </html-el:form>
              </c:forEach>
            </c:otherwise>
          </c:choose>

          <html-el:form action="/bp/reportFixedDataAdd.do">
            <html-el:hidden property="chkptType" value="${view.chkptType}"/>
            <html-el:hidden property="taskType" value="${taskType}"/>
            <html-el:hidden property="taskIndex" value="${taskIndex}"/>
            <html-el:hidden property="procIndex" value="${procIndex}"/>
            <html-el:hidden property="flowIndex" value="${flowIndex}"/>
            <tr>
              <td headers="Label:Fixed" align="left">
                <html-el:text property="dataLabel" value=""/>
              </td>
              <td headers="Value:Fixed" align="left">
                <html-el:text property="dataValue" value=""/>
              </td>
              <td headers="Actions:Fixed" align="left" nowrap>
                <html-el:submit
                            styleClass="inputButton"
                            property="submitBtn"
                            onclick="return doAddFixedData(this.form);">
                  <bean-el:message key="button.add" bundle="bp.messages"/>
                </html-el:submit>
              </td>
            </tr>
          </html-el:form>
        </table>
        <div>&nbsp;</div>
      </c:if>

      <!--
        ************************************************************
        ** Variable data table.
        -->
      <c:if test="${!empty view.captureInfo}">
        <div class="pageSubtitle">
          <bean-el:message
            key="bp.report.taskEdit.varData.title" bundle="bp.messages"
          />
        </div>
        <table class="dataTable" border="0" cellspacing="1" cellpadding="2"
              summary="variable data" width="100%">
          <tr class="listHeader">
            <th id="Label:Var" align="left">
              <bean-el:message
                key="bp.report.taskEdit.varData.header.label"
                bundle="bp.messages"
              />
            </th>
            <th id="Name:Var" align="left">
              <bean-el:message
                key="bp.report.taskEdit.varData.header.${taskType}.name"
                bundle="bp.messages"
              />
            </th>
            <th id="Actions:Var" align="left" nowrap>
              <bean-el:message key="header.actions" bundle="bp.messages"/>
            </th>
          </tr>

          <%
          final List variableList = (List)view.get("variableList");
          List availableVars = null;
          if (varData != null && !varData.isEmpty()) {
            pageContext.setAttribute("varData", new TreeMap(varData));
            availableVars = new ArrayList(variableList.size());
            final Iterator iter = variableList.iterator();
            while (iter.hasNext()) {
              final String v = (String)iter.next();
              if (varData.get(v) == null)
                availableVars.add(v);
            }
          }
          else {
            pageContext.removeAttribute("varData");
            availableVars = variableList;
          }
          if (availableVars == null || availableVars.size() == 0)
            pageContext.removeAttribute("availableVars");
          else
            pageContext.setAttribute("availableVars", availableVars);
          %>
          <c:choose>
            <c:when test="${!empty varData}">
              <c:forEach var="entry" items="${varData}" varStatus="index">
                <html-el:form action="/bp/reportVariableUpdate.do">
                  <html-el:hidden
                    property="chkptType" value="${view.chkptType}"
                  />
                  <html-el:hidden property="taskType" value="${taskType}"/>
                  <html-el:hidden property="taskIndex" value="${taskIndex}"/>
                  <html-el:hidden property="procIndex" value="${procIndex}"/>
                  <html-el:hidden property="flowIndex" value="${flowIndex}"/>
                  <html-el:hidden property="varName" value="${entry.key}"/>
                  <tr class="<c:out value="${listSwitch[(index.count-1)%2]}"/>">
                    <td headers="Label:Var">
                      <html-el:text property="varLabel" value="${entry.value}"/>
                    </td>
                    <td headers="Name:Var"><c:out value="${entry.key}"/></td>
                    <td headers="Actions:Var" nowrap>
                      <html-el:submit styleClass="inputButton" property="upd">
                        <bean-el:message
                          key="button.update" bundle="bp.messages"
                        />
                      </html-el:submit>
                      <html-el:submit styleClass="inputButton" property="del"
                                      onclick="doDelVariable(this.form);">
                        <bean-el:message
                          key="button.delete" bundle="bp.messages"
                        />
                      </html-el:submit>
                    </td>
                  </tr>
                </html-el:form>
              </c:forEach>
            </c:when>
            <c:otherwise>
              <tr class="listEven">
                <td colspan="3" align="center">
                  <bean-el:message
                    key="bp.report.taskEdit.varData.none" bundle="bp.messages"
                  />
                </td>
              </tr>
            </c:otherwise>
          </c:choose>

          <c:choose>
            <c:when test="${!empty availableVars}">
              <html-el:form action="/bp/reportVariableAdd.do">
                <html-el:hidden property="chkptType" value="${view.chkptType}"/>
                <html-el:hidden property="taskType" value="${taskType}"/>
                <html-el:hidden property="taskIndex" value="${taskIndex}"/>
                <html-el:hidden property="procIndex" value="${procIndex}"/>
                <html-el:hidden property="flowIndex" value="${flowIndex}"/>
                <tr>
                  <td headers="Label:Var" align="left">
                    <html-el:text property="varLabel" value=""/>
                  </td>
                  <td headers="Name:Var" align="left">
                    <html-el:select property="varName">
                      <c:forEach var="var" items="${availableVars}">
                        <html-el:option value="${var}"/>
                      </c:forEach>
                    </html-el:select>
                  </td>
                  <td headers="Actions:Var" align="left" nowrap>
                    <html-el:submit
                                styleClass="inputButton"
                                property="submit"
                                onclick="return doAddVariable(this.form);">
                      <bean-el:message key="button.add" bundle="bp.messages"/>
                    </html-el:submit>
                  </td>
                </tr>
              </html-el:form>
            </c:when>
            <c:otherwise>
              <tr>
                <td colspan="3">
                  <bean-el:message
                    key="bp.report.taskEdit.varData.${taskType}.noMoreAvailable"
                    bundle="bp.messages"
                  />
                </td>
              </tr>
            </c:otherwise>
          </c:choose>
        </table>
      </c:if>
    </td>
  </tr>

</table>

<!-- =============================================================
  ** vim: set ts=2 expandtab syntax=jsp:
  -->
